package com.example.maze.util;

import com.example.maze.model.Cell;
import com.example.maze.model.Maze;
import com.example.maze.solver.SolverResult;

import java.util.List;

/**
 * ASCII可视化工具类，用于在控制台显示迷宫
 * 
 * @author maze-demo
 */
public class MazeVisualizer {
    
    // 不同类型单元格的显示字符
    private static final char WALL_CHAR = '█';        // 墙
    private static final char PATH_CHAR = ' ';        // 通路
    private static final char START_CHAR = 'S';       // 起点
    private static final char END_CHAR = 'E';         // 终点
    private static final char VISITED_CHAR = '.';     // 已访问
    private static final char SOLUTION_CHAR = '*';    // 解路径
    private static final char DFS_PATH_CHAR = 'D';    // DFS路径
    private static final char BFS_PATH_CHAR = 'B';    // BFS路径
    private static final char ASTAR_PATH_CHAR = 'A';  // A*路径
    
    /**
     * 显示迷宫的ASCII表示
     * 
     * @param maze 要显示的迷宫
     */
    public static void printMaze(Maze maze) {
        System.out.println(generateMazeString(maze));
    }

    /**
     * 生成迷宫的ASCII字符串表示
     * 
     * @param maze 要转换的迷宫
     * @return ASCII字符串
     */
    public static String generateMazeString(Maze maze) {
        StringBuilder sb = new StringBuilder();
        
        // 添加顶部边框
        sb.append("┌");
        for (int col = 0; col < maze.getCols(); col++) {
            sb.append("─");
        }
        sb.append("┐\n");
        
        // 生成迷宫内容
        for (int row = 0; row < maze.getRows(); row++) {
            sb.append("│"); // 左边框
            
            for (int col = 0; col < maze.getCols(); col++) {
                Cell cell = maze.getCell(row, col);
                sb.append(getCellChar(cell));
            }
            
            sb.append("│\n"); // 右边框
        }
        
        // 添加底部边框
        sb.append("└");
        for (int col = 0; col < maze.getCols(); col++) {
            sb.append("─");
        }
        sb.append("┘");
        
        return sb.toString();
    }

    /**
     * 获取单元格的显示字符
     */
    private static char getCellChar(Cell cell) {
        switch (cell.getType()) {
            case WALL:
                return WALL_CHAR;
            case PATH:
                return PATH_CHAR;
            case START:
                return START_CHAR;
            case END:
                return END_CHAR;
            case VISITED:
                return VISITED_CHAR;
            case SOLUTION:
                return SOLUTION_CHAR;
            case DFS_PATH:
                return DFS_PATH_CHAR;
            case BFS_PATH:
                return BFS_PATH_CHAR;
            case ASTAR_PATH:
                return ASTAR_PATH_CHAR;
            default:
                return PATH_CHAR;
        }
    }

    /**
     * 打印寻路结果的对比
     * 
     * @param results 多个算法的寻路结果
     */
    public static void printSolverComparison(List<SolverResult> results) {
        System.out.println("\n=== 寻路算法对比 ===");
        System.out.printf("%-8s %-8s %-8s %-8s %-8s%n", 
            "算法", "状态", "路径长度", "访问节点", "耗时(ms)");
        System.out.println("────────────────────────────────────────────");
        
        for (SolverResult result : results) {
            System.out.printf("%-8s %-8s %-8d %-8d %-8d%n",
                result.getAlgorithmName(),
                result.isFound() ? "成功" : "失败",
                result.getPathLength(),
                result.getVisitedCells(),
                result.getTimeMillis());
        }
        System.out.println();
    }

    /**
     * 打印迷宫信息头部
     * 
     * @param maze 迷宫对象
     * @param generatorName 生成器名称
     */
    public static void printMazeHeader(Maze maze, String generatorName) {
        System.out.println(repeatString("=", 50));
        System.out.printf("迷宫大小: %d x %d%n", maze.getRows(), maze.getCols());
        System.out.printf("生成算法: %s%n", generatorName);
        System.out.printf("起点: %s%n", maze.getStart());
        System.out.printf("终点: %s%n", maze.getEnd());
        System.out.println(repeatString("=", 50));
    }

    /**
     * 打印图例
     */
    public static void printLegend() {
        System.out.println("\n=== 图例 ===");
        System.out.printf("%c - 墙%n", WALL_CHAR);
        System.out.printf("%c - 通路%n", PATH_CHAR);
        System.out.printf("%c - 起点%n", START_CHAR);
        System.out.printf("%c - 终点%n", END_CHAR);
        System.out.printf("%c - 已访问%n", VISITED_CHAR);
        System.out.printf("%c - 通用解路径%n", SOLUTION_CHAR);
        System.out.printf("%c - DFS路径%n", DFS_PATH_CHAR);
        System.out.printf("%c - BFS路径%n", BFS_PATH_CHAR);
        System.out.printf("%c - A*路径%n", ASTAR_PATH_CHAR);
        System.out.println();
    }

    /**
     * 显示带标题的迷宫
     * 
     * @param maze 迷宫对象
     * @param title 标题
     */
    public static void printMazeWithTitle(Maze maze, String title) {
        System.out.println("\n" + title);
        System.out.println(repeatString("-", title.length()));
        printMaze(maze);
    }

    /**
     * 标记特定算法的路径
     * 
     * @param maze 迷宫对象
     * @param path 路径坐标列表
     * @param algorithmName 算法名称
     */
    public static void markAlgorithmPath(Maze maze, java.util.List<com.example.maze.model.Coordinate> path, String algorithmName) {
        if (path == null || path.isEmpty()) {
            return;
        }
        
        com.example.maze.model.Cell.Type pathType;
        switch (algorithmName.toUpperCase()) {
            case "DFS":
                pathType = com.example.maze.model.Cell.Type.DFS_PATH;
                break;
            case "BFS":
                pathType = com.example.maze.model.Cell.Type.BFS_PATH;
                break;
            case "A*":
                pathType = com.example.maze.model.Cell.Type.ASTAR_PATH;
                break;
            default:
                pathType = com.example.maze.model.Cell.Type.SOLUTION;
                break;
        }
        
        for (com.example.maze.model.Coordinate coord : path) {
            com.example.maze.model.Cell cell = maze.getCell(coord);
            if (cell.getType() != com.example.maze.model.Cell.Type.START && 
                cell.getType() != com.example.maze.model.Cell.Type.END) {
                cell.setType(pathType);
            }
        }
    }

    /**
     * 生成简化的迷宫字符串（用于README等）
     * 使用更简单的字符
     */
    public static String generateSimpleMazeString(Maze maze) {
        StringBuilder sb = new StringBuilder();
        
        for (int row = 0; row < maze.getRows(); row++) {
            for (int col = 0; col < maze.getCols(); col++) {
                Cell cell = maze.getCell(row, col);
                switch (cell.getType()) {
                    case WALL:
                        sb.append("██");
                        break;
                    case START:
                        sb.append("S ");
                        break;
                    case END:
                        sb.append("E ");
                        break;
                    case SOLUTION:
                        sb.append("* ");
                        break;
                    case VISITED:
                        sb.append(". ");
                        break;
                    default:
                        sb.append("  ");
                        break;
                }
            }
            sb.append("\n");
        }
        
        return sb.toString();
    }

    /**
     * 重复字符串（Java 8兼容）
     */
    private static String repeatString(String str, int count) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < count; i++) {
            sb.append(str);
        }
        return sb.toString();
    }
}